#################################################################################### 
####################################################################################
####################################################################################
# Main Text: Figure 1
####################################################################################
####################################################################################
library(here)
library(dplyr)
library(tidyr)
library(ggplot2)
####################################################################################
####################################################################################
## Read in survey data (from: power_dist_dk.R)
dat = read.csv(here("data/dk_responses.csv"))

####################################################################################
# Increase in perceptions of power b/w T/C

d2 <- dat[c("power_csk","power_csd","power_ngos","power_pres","power_mp","power_dc","power_lc", "treatment")] %>% 
  gather(., key = "key", value = "value", -one_of("treatment")) %>%
  group_by(key) %>%
  filter(value == 4) %>%
  group_by(key, treatment) %>%
  summarise(count=n())  

d2$count = as.numeric(d2$count)
d2[d2$treatment == 1,]$count = as.numeric( (d2[d2$treatment == 1,]$count / nrow(dat[dat$treatment == 1,])) )
d2[d2$treatment == 0,]$count = as.numeric(d2[d2$treatment == 0,]$count / nrow(dat[dat$treatment == 0,]))

d2$treatment = factor(d2$treatment, levels = c(0,1),
                      labels = c("Control", "Treatment")) 

pres_power = ggplot(d2, aes(x = reorder(key, -count), y = count*100)) +
  geom_bar(stat="identity", position = "dodge") +
  theme_bw() + facet_wrap(~treatment) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(d2$count*100)+1), labels=function(x) paste0(x,"%")) +
  scale_x_discrete(labels= c("The\nPresident", "NGOs", "Members of\nParliament", "District\nCouncilors", "Civil Servants\nKampala", 
                             "Civil Servants\nDistrict", "Local\nCouncilors")) + 
  geom_text(aes(x = key, y = (count*100)-2.5), color="#000000", label = round(d2$count*100, 0), 
            angle=0, vjust = 0, parse = T,  size = 4, check_overlap = TRUE) + 
  labs(#title = "Share of Respondents Reporting Actor Has \"A lot\" of Power\nOver NGO Project Locations",
       x = NULL,
       y = "Percent") + 
  theme(legend.title=element_blank(),
        plot.title = element_text(size=14,hjust = 0.5),
        legend.position = c(0.1, 0.9),
        legend.background = element_blank(),
        axis.text.x = element_text(size=7))

ggsave(file = here("plots/figure_1.tiff"), pres_power, width = 10, height = 6, dpi=700)
ggsave(file = here("plots/power_alot_treatment_wrap.pdf"), pres_power, width = 10, height = 6)